package org.xinjiang.tarim.travel.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xinjiang.tarim.travel.mapper.PoiMapper;
import org.xinjiang.tarim.travel.model.Poi;
import org.xinjiang.tarim.travel.model.PoiDetail;
import org.xinjiang.tarim.travel.service.AttentionMddService;
import org.xinjiang.tarim.travel.service.PoiDetailService;
import org.xinjiang.tarim.travel.service.PoiService;
import org.xinjiang.tarim.travel.vo.Criteria;
import org.xinjiang.tarim.travel.vo.PageBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PoiServiceImpl implements PoiService {
    @Autowired
    PoiMapper poiMapper;
    @Autowired
    PoiDetailService poiDetailService;
    @Autowired
    AttentionMddService attentionMddService;

    @Override
    public List<Poi> selectByMddId(String mddId) {
        return poiMapper.selectByMddId(mddId);
    }

    @Override
    public List<Poi> selectAllByMddId(Poi poi) {
        return poiMapper.selectAllByMddId(poi);
    }

    @Override
    public List<Poi> selectTenPoi() {
        return poiMapper.selectTenPoi();
    }

    @Override
    public Map selectByPrimaryKey(String poiId) {
        Poi poi = poiMapper.selectByPrimaryKey(poiId);
        PoiDetail poiDetail = poiDetailService.selectByPrimaryKey(poiId);
        Map map = new HashMap();
        map.put("poi", poi);
        map.put("poiDetail", poiDetail);
        return map;
    }

    @Override
    public PageBean selectConcerns(Criteria criteria, PageBean pageBean) {
        List list = attentionMddService.selectByOpenid(criteria);
        if(list.size() == 0){
            return pageBean;
        }

        criteria.addExtField("pageIndex", (pageBean.getPageIndex()-1) * pageBean.getPageSize());
        criteria.addExtField("pageSize", pageBean.getPageSize());
        criteria.addExtField("list", list);
        Long totalCount = poiMapper.selectByMddIdsCount(criteria);
        List<Poi> resultList = poiMapper.selectByMddIds(criteria);
        List<Map> mapList = new ArrayList<>();
        for(Poi poi : resultList){
            Map map = new HashMap();
            PoiDetail poiDetail = poiDetailService.selectByPrimaryKey(poi.getPoiId());
            map.put("poi", poi);
            map.put("poiDetail", poiDetail);
            mapList.add(map);
        }
        pageBean.setTotalCount(totalCount);
        pageBean.setResultList(mapList);
        return pageBean;
    }
}
